#include "PF_Config.h"
unsigned char m1=0x00;
unsigned char m2=0x00;
unsigned char m3=0x00;
unsigned char m4=0x00;
unsigned char m5=0x00;
//unsigned char beq_long=0x00; //key_long--beq
unsigned char cs_turn=0x00; //
//unsigned char cs_back=0x00;
unsigned char time_5ms=0x00;
unsigned char led_beq=0x00; //0 beq 1 led1 2 led2 3 cha
unsigned char time_cd=250;
unsigned char dis_5s=250;

void g_gas(void)
{
	int g=0x0000;
	unsigned int g_o2=0x00;
//	g=g_wADCValue-value.value_zero;
	switch(value.dir)
	{
		case 0:
			g=value.g_wADCValue-value.value_zero;
			if(g>threshold) value.value_gas=(unsigned int)(((unsigned long)g*(unsigned long)value.value_multiple)>>7);
			else value.value_gas=0x0000;
		break;
		case 1:
			g=value.value_zero-value.g_wADCValue;
			if(g>threshold) value.value_gas=(unsigned int)(((unsigned long)g*(unsigned long)value.value_multiple)>>7);
			else value.value_gas=0x0000;
		break;
		case 2:
			if((value.g_wADCValue+threshold)<value.value_zero)
			{
				g_o2=(unsigned int)(((unsigned long)(value.value_zero-value.g_wADCValue)*(unsigned long)value.value_multiple)>>7);
				if(g_o2>=gas0) g_o2=gas0;
				value.value_gas=gas0-g_o2;
			}
			else if(value.g_wADCValue>(threshold+value.value_zero)) value.value_gas=gas0+(unsigned int)(((unsigned long)(value.g_wADCValue-value.value_zero)*(unsigned long)value.value_multiple)>>7);
			else value.value_gas=gas0; //209
//			if((value.g_wADCValue+threshold)<value.value_zero) value.value_gas=(unsigned int)(((unsigned long)(value.value_zero-value.g_wADCValue)*(unsigned long)value.value_multiple)>>7);
//			else if((value.g_wADCValue-threshold)>value.value_zero) value.value_gas=(unsigned int)(((unsigned long)(value.g_wADCValue-value.value_zero)*(unsigned long)value.value_multiple)>>7);
//			else value.value_gas=0x00d1; //209
		break;
	}
}

unsigned char fun_cd(void)
{
	if(time_cd==0x00)
	{
		time_cd=250;
		return 1;
	}
	else --time_cd;
	if((key_short)||(key_cont)||(key_long)) time_cd=250;
	return 0;
}

unsigned char fun_5s(void)
{
	dis_5s--;
	if(!(dis_5s))
	{
		dis_5s=250;
		return 1;
	}
	if((dis_5s%50)==49) beq_5s=0x01;
	if(dis_5s==249) beq_5s=0x00;
	if((dis_5s%50)==39) beq_5s=0x00;
	if(dis_on)
	{
		turn_add(dis_5s/50);
		dis[1]();
	}
	return 0;
}

void fun_key_long(unsigned char m)
{
	switch(m)
	{
		case 01: //25: //B+
			beq_long=0x01;
			led1_s_w_on;
			led2_s_w_on;
		break;
		case 11://28: //B-
			beq_long=0x00;
			led1_s_w_off;
			led2_s_w_off;
		break;
		case 51://50: //B+
			beq_long=0x01;
			led1_s_w_on;
			led2_s_w_on;
		break;
		case 61://53: //B-
			beq_long=0x00;
			led1_s_w_off;
			led2_s_w_off;
		break;
		case 101://75: //B+
			beq_long=0x01;
			led1_s_w_on;
			led2_s_w_on;
		break;
		case 111://78: //B-
			beq_long=0x00;
			led1_s_w_off;
			led2_s_w_off;
			cs_turn=0x01;
		break;
	}
}

unsigned char fun_yes(unsigned char dis_0se_gas)
{
	if(key_short)
	{
		dis_on=0x01;
		bf=0x01;
		Display_clear();
	}
	switch(key_short)
	{
		case 0b00000010:
			yes_no=0x01;
		break;
		case 0b00000100:
			yes_no=0x00;
		break;
		case 0b00000001:
			cs_turn=0x01;
		break;
	}
	if(dis_on)
	{
		ad_off(dis_0se_gas);
		dis[1]();
		if(bf)
		{
			ad_off(yes_no);
			dis[0]();
		}
		bf^=0x01;
	}
	if(cs_turn)
	{
		if(yes_no) return 3;
		else return 4;
	}
	else return 5;
}

unsigned char fun_countdown(unsigned int c)
{
	static unsigned char time_0se=0x03;
	dis_5s--;
	if(dis_5s==0x00)
	{
		dis_5s=250;
		time_0se--;
	}
	if((time_0se==0x00)&&((dis_5s%50)==49)) value.value_sum+=c;
	if((time_0se==0x00)&&(dis_5s==0x01))
	{
		time_0se=0x03;
		dis_5s=250;
		return 1;
	}
	else
	{
		if((dis_5s%50)==49) beq_5s=0x01;
		if((dis_5s%50)==39) beq_5s=0x00;
	}
	if(dis_on)
	{
		turn_add((5*time_0se)+((dis_5s)/50));
		dis[1]();
		if(state_control&0b00000001)
		{
			if(cs==0x20) turn_add(c);
			else turn_10(c);
			dis[0]();

		}
		lel_vol_ppm; //%LEL
	}
	return 0;
}

void fun_low(unsigned char low_high_dir) // low_high_dir 0 low_high=low 1 low_high=high
{
	if((key_short)||(key_cont))
	{
		dis_on=0x01;
		Display_clear();
	}
	if((key_short==0b00000010)||(key_cont==0b00000010))
	{
		if(!low_high_dir)
		{
			if((value.dir==0)||(value.dir==1))
			{
				if(value.value_low<value.value_high) value.value_low++;
			}
			else if(value.dir==2)
			{
				if(value.value_low<a0) value.value_low++;
			}
		}
		else
		{
			if((value.dir==0)||(value.dir==1))
			{
				if(value.value_high<a0) value.value_high++;
			}
			else if(value.dir==2)
			{
				if(value.value_high<value.value_low) value.value_high++;
			}
		}
	}
	if((key_short==0b00000100)||(key_cont==0b00000100))
	{
		if(low_high_dir)
		{
			if((value.dir==0)||(value.dir==1))
			{
				if(value.value_high>value.value_low) value.value_high--;
			}
			else if(value.dir==2)
			{
				if(value.value_high>0) value.value_high--;
			}

		}
		else
		{
			if((value.dir==0)||(value.dir==1))
			{
				if(value.value_low>0) value.value_low--;
			}
			else if(value.dir==2)
			{
				if(value.value_low>value.value_high) value.value_low--;
			}
		}
	}
	if((key_short==0b00000001)||(key_cont==0b00000001))
	{
		if(low_high_dir)
		{
			cs=0x4a;
		}
		else
		{
			cs=0x48;
		}
	}
	if(dis_on)
	{
		if(!Cont) bf^=0x01;
		else bf=0x01;
		if(bf)
		{
			if(low_high_dir)
			{
				high_control=0x02;
				low_control=0x02;
				turn_10(value.value_high);
			}
			else
			{
				low_control=0x02;
				turn_10(value.value_low);
			}
			dis[0]();
		}
//	point_on=point_on_s;
	}
}

void alarm_low(void)
{
	switch(alarm_add)
	{
		case 00: //L1+ L2- B+
			led1_s_w_on;
			led2_s_w_off;
			beq_alarm=0x01;
		break;
		case 05: //L1+ L2- B+
			led1_s_w_on;
			led2_s_w_off;
			beq_alarm=0x01;
		break;
		case 10: //L1- L2- B-
			led1_s_w_off;
			led2_s_w_off;
			beq_alarm=0x00;
		break;
		case 15: //L1- L2- B-
			led1_s_w_off;
			led2_s_w_off;
			beq_alarm=0x00;
		break;
		case 20: //L1- L2+ B+
			led1_s_w_off;
			led2_s_w_on;
			beq_alarm=0x01;
		break;
		case 25: //L1- L2+ B+
			led1_s_w_off;
			led2_s_w_on;
			beq_alarm=0x01;
		break;
		case 30: //L1- L2- B-
			led1_s_w_off;
			led2_s_w_off;
			beq_alarm=0x00;
		break;
		case 35: //L1- L2- B-
			led1_s_w_off;
			led2_s_w_off;
			beq_alarm=0x00;
		break;
   }
}
void alarm_high(void)
{
	switch(alarm_add)
	{
		case 00: //L1+ L2- B+
			led1_s_w_on;
			led2_s_w_off;
			beq_alarm=0x01;
		break;
		case 05: //L1- L2- B-
			led1_s_w_off;
			led2_s_w_off;
			beq_alarm=0x00;
		break;
		case 10: //L1- L2+ B+
			led1_s_w_off;
			led2_s_w_on;
			beq_alarm=0x01;
		break;
		case 15: //L1- L2- B-
			led1_s_w_off;
			led2_s_w_off;
			beq_alarm=0x00;
		break;
		case 20: //L1+ L2- B+
			led1_s_w_on;
			led2_s_w_off;
			beq_alarm=0x01;
		break;
		case 25: //L1- L2- B-
			led1_s_w_off;
			led2_s_w_off;
			beq_alarm=0x00;
		break;
		case 30: //L1- L2+ B+
			led1_s_w_off;
			led2_s_w_on;
			beq_alarm=0x01;
		break;
		case 35: //L1- L2- B-
			led1_s_w_off;
			led2_s_w_off;
			beq_alarm=0x00;
		break;
	}
}
